<?php


class TopicService extends Service
{
  /**
   * @param string $className
   * @return TopicService
   */
  public static function instance($className = __CLASS__)
  {
    return parent::instance($className);
  }

  /**
   * 发帖子
   * @param array $data
   * @return array|bool
   */
  public function post($data = array())
  {
    $key = 'user-id-post-topic-' . Yii::app()->user->id;
    if (Yii::app()->cache->get($key) != false) {
      return '发帖间隔必须大于' . Yii::app()->params['interval'] . '秒';
    }
    $topic = new Topic();
    $topic->attributes = $data;
    if ($topic->save()) {
      //写入发帖时间
      Yii::app()->cache->set($key, 1, Yii::app()->params['interval']);
      return $topic->topic_id;
    } else {
      return $topic->errors;
    }
  }

  /**
   * 点击+1
   * @param int $id
   */
  public function plus($id = 0)
  {
    $key = 'ip-' . Yii::app()->request->userHostAddress . '-id-' . $id;
    $data = Yii::app()->cache->get($key);
    if ($data === false) {
      $topic = Topic::model()->findByPk($id);
      $topic->hits++;
      $topic->save();
      Yii::app()->cache->set($key, 1);
    }
  }

  /**
   * 获取主题
   * @param int $id
   * @return CActiveRecord|mixed
   */
  public function get($id = 0)
  {
    $key = 'topic-' . $id;
    $data = Yii::app()->cache->get($key);
    if ($data === false) {
      $criteria = new CDbCriteria();
      $criteria->condition = 'topic_id=:topic_id';
      $criteria->params = array(
          ':topic_id' => $id
      );
      $criteria->with = array(
          'user'
      );
      $data = Topic::model()->find($criteria);
      Yii::app()->cache->set($key, $data, 3600);
    }
    return $data;
  }

  /**
   * 回复帖子
   * @param array $data
   * @return array|bool
   */
  public function reply($data = array())
  {
    $thread = new Threads();
    $thread->attributes = $data;
    return $thread->save() ? true : $thread->errors;
  }

  /**
   * 获取帖子回帖列表
   * @param int $topic_id
   * @param int $pageSize
   * @param bool $with
   * @return array
   */
  public function getThreadList($topic_id = 0, $pageSize = 20, $with = true)
  {
    $criteria = new CDbCriteria();
    $criteria->condition = 'threads.topic_id=:topic_id and pid = 0';
    $criteria->params = array(':topic_id' => $topic_id);
    //关联
    if ($with) {
      $criteria->with = array(
          'topic',
          'user'
      );
    } else {
      $criteria->with = array(
          'topic'
      );
    }
    $criteria->order = 'threads.thread_id asc';
    $count = Threads::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);
    $criteria->offset = $pages->currentPage * $pages->pageSize;
    $list = Threads::model()->findAll($criteria);
    return array(
        'list' => $list,
        'pages' => $pages
    );
  }

  /**
   * 获取主题列表
   * @param int $pageSize
   * @param bool $with
   * @param string $keywords
   * @param int $user_id
   * @return array
   */
  public function getTopicList($pageSize = 20, $with = true, $keywords = '', $user_id = 0)
  {
    $criteria = new CDbCriteria();
    if ($user_id > 0) {
      $criteria->condition = 'topic.user_id=:user_id';
      $criteria->params = array(':user_id' => $user_id);
    }
    //关联
    if ($with) {
      $criteria->with = array(
          'user'
      );
    }
    if (!empty($keywords)) {
      $criteria->addSearchCondition('title', $keywords);
    }
    $criteria->order = 'topic.hits desc,topic.created_at desc';
    $count = Topic::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);
    $criteria->offset = $pages->currentPage * $pages->pageSize;
    $list = Topic::model()->findAll($criteria);
    return array(
        'list' => $list,
        'pages' => $pages
    );
  }

  /**
   * 删除主题
   * @param array $list
   * @return bool
   */
  public function delete($list = array())
  {
    $criteria = new CDbCriteria();
    $criteria->addInCondition('topic_id', $list);
    return Topic::model()->deleteAll($criteria) ? true : false;
  }

  /**
   * 获取用户回复
   * @param int $user_id
   * @param int $pageSize
   * @param string $keywords
   * @return array
   */
  public function getUserThreads($user_id = 0, $pageSize = 20, $keywords = '')
  {
    $criteria = new CDbCriteria();
    $criteria->with = array(
        'topic'
    );
    $criteria->condition = 'threads.topic_id=topic.topic_id and threads.user_id=:user_id';
    $criteria->params = array(
        ':user_id' => $user_id
    );
    if ($keywords) {
      $criteria->addSearchCondition('threads.content', $keywords);
    }
    $count = Threads::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);

    $criteria->offset = $pages->currentPage * $pageSize;
    $criteria->limit = $pageSize;
    $criteria->order = 'threads.thread_id desc';
    $list = Threads::model()->findAll($criteria);
    return array(
        'list' => $list,
        'pages' => $pages
    );
  }

  /**
   * 删除回复
   * @param array $list
   * @return bool
   */
  public function deleteThread($list = array())
  {
    $criteria = new CDbCriteria();
    $criteria->addInCondition('thread_id', $list);
    return Threads::model()->deleteAll($criteria) ? true : false;
  }

  /**
   * 获取所有回帖
   * @param int $pageSize
   * @param string $keywords
   * @return array
   */
  public function getAllThreadList($pageSize = 20, $keywords = '')
  {
    $criteria = new CDbCriteria();
    $criteria->with = array(
        'topic',
        'user'
    );
    if (!empty($keywords)) {
      $criteria->addSearchCondition('threads.content', $keywords);
    }
    $criteria->order = 'threads.thread_id asc';
    $count = Threads::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);
    $criteria->offset = $pages->currentPage * $pages->pageSize;
    $list = Threads::model()->findAll($criteria);
    return array(
        'list' => $list,
        'pages' => $pages
    );
  }
} 